package br.ucb.cm.config.page.integration.ui;

import javax.swing.JPanel;

import br.ucb.cm.board.ui.BoardController;
import br.ucb.cm.board.ui.PieceDisposicionPane;
import br.ucb.cm.config.bean.Config;
import br.ucb.cm.config.bean.PieceConfig;
import br.ucb.cm.main.common.GeneralConstants;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.FlowLayout;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class IntegrationUI extends JPanel implements BoardController {
	private static final long serialVersionUID = 1L;
	private Palette palette;
	private Config config;
	private PieceDisposicionPane boardPane;
	private JPanel panel;
	private JRadioButton rdbtnEspelhado;
	private JRadioButton rdbtnInvertido;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	public IntegrationUI() {
		initComponents();
	}

	private void initComponents() {
		palette = new Palette(null);
		
		JPanel panelBoard = new JPanel();
		panelBoard.setLayout(new BoxLayout(panelBoard, BoxLayout.Y_AXIS));
		
		boardPane = new PieceDisposicionPane(GeneralConstants.MIN_COLUMNS, GeneralConstants.MIN_ROWS, this);
		panelBoard.add(boardPane);
		
		panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Disposi\u00E7\u00E3o das pe\u00E7as do advers\u00E1rio", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		rdbtnEspelhado = new JRadioButton("Espelhada");
		rdbtnEspelhado.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boardPane.setMirrored(rdbtnEspelhado.isSelected());
			}
		});
		rdbtnEspelhado.setSelected(true);
		buttonGroup.add(rdbtnEspelhado);
		panel.add(rdbtnEspelhado);
		
		rdbtnInvertido = new JRadioButton("Invertida");
		rdbtnInvertido.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boardPane.setMirrored(rdbtnEspelhado.isSelected());
			}
		});
		buttonGroup.add(rdbtnInvertido);
		panel.add(rdbtnInvertido);
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(panel, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
				.addComponent(palette, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
				.addComponent(panelBoard, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panelBoard, GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(3)
					.addComponent(palette, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
		);
		setLayout(groupLayout);
	}
	
	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
		palette.setPieceList(config.getPieces());
		boardPane.applyBoardConfig(config.getBoard());
	}
	
	@Override
	public PieceConfig doAction(ActionEnum action) {
		PieceConfig value = palette.getValue();
		
		return value;
	}

	public Object getValue() {
		return boardPane.getPieceMap();
	}

	public boolean isEmpty() {
		return boardPane.isEmpty();
	}

	@Override
	public Object doAction(ActionEnum action, Object object) {
		return null;
	}

}
